/**
 * Created by Administrator on 2017/5/18.
 */
 var linkurl = "http://"+window.location.host+"/";//获取当前域名
 var  id=$('#getID').val();
 var countpage=0;
 var page =1;
    $(function () {
	//右侧导航点击回到头部
	 $("#sidebar-c").click(function(){
	                $('body,html').animate({scrollTop:0},1000);
	                return false;
	            });
     $.ajax({
         type : "get",
         url : linkurl + "isLoged",
         data: {},
         dataType: 'json',
         success:function (Result) {
             var result = Result.result;
             if(result==1){
                 $(".pcc_gerenbnt").removeClass("hide");
                 $("#to-login").addClass("hide");
                 $("#to-reg").addClass("hide");
             }
         }
     });
     $("#pcc-logout").click(function () {
         $.ajax({
             type : "post",
             url : linkurl + "logout",
             data: {},
             dataType: 'json',
             success:function () {
                 window.location.href="index.html";
             }
         })
     })
       readpinlun(page);
     if(countpage!=0){
         // 分页按钮加载
         $("#demo1").paginate({
             count       : countpage,
             start       : 1,
             display     : 10,
             border                  : true,
             border_color            : '#cecece',
             text_color              : '#666666',
             background_color        : '#ffffff',
             border_hover_color      : '#ff8420',
             text_hover_color        : 'ff8420',
             background_hover_color  : '#ffffff',
             rotate      : false,
             images      : false,
             mouse       : 'press'
         });
     }

     $.ajax({
         type: 'get',
         url : linkurl + "getXunrenClassifyList",
         data: {},
         dataType: 'json',
         success: function (Result) {
             var  data = Result.data;
             $("#pcc-classify").html("");
             $.each(data, function (index,item) {
                 $("#pcc-classify").append("<a href='../d-"+item.id+"/'>"+item.className+"</a>")
             })
         }
     });
      $.ajax({
         type: 'get',
         url : linkurl + "modifyxunrenhits",
         data: {id:id},
         dataType: 'json',
         success: function (Result) { 
                $('#readhits').html("阅读量："+Result.data);			
         }
     });
		$.ajax({
         type: 'get',
         url : linkurl + "selectPinlunCountID",
         data: {id:id,types:2},
         dataType: 'json',
         success: function (Result) { 
                $('#pingluncount').html("评论（"+Result.data+"）");
         }
     }); 
		
		 $.ajax({
		        type : "get",
		        url : linkurl + "getzuixinXunrenList",
		        data: {pagesize:3},
		        dataType: 'json',
		        success:function (Result) {
		            var data = Result.data;
		            $("#pcc-foot-x").html("");
		            $.each(data, function (index,item) {
		                $("#pcc-foot-x").append("<div class='pcc-articlefool' ><a class='live_xunren_list' href='../"+item.url+"/"+item.linkUrl+"'>["+item.provinceName+"]"+item.title+"</a>" +
		                    ""+item.content+"</div>");
		            });
		        }
		    });
		    $.ajax({
		        type : "get",
		        url : linkurl + "getTopDiqu",
		        data: {},
		        dataType: 'json',
		        success:function (Result) {
		            var data = Result.data;
		            $("#pcc-foot-c").html("");
		            $.each(data,function (index,item) {
		                $("#pcc-foot-c").append("<a class='pcc_fooldiquxunren' href='../a-"+item.url+"/'>"+item.diquName+"寻人</a>")
		            })
		        }
		    });
    $(".search_button3").click(function () {
        var search = $("#search_key").val();
        if(search!=""){
            window.location.href = "../sousuojieguo.html?search=" + search;
        }
    });
   
     $("#pcc-logout").click(function () {
         $.ajax({
             type : "post",
             url : linkurl + "logout",
             data: {},
             dataType: 'json',
             success:function () {
                 $(".pcc_gerenbnt").addClass("hide");
                 $("#to-login").removeClass("hide");
                 $("#to-reg").removeClass("hide");
                 $(".need-login").removeClass("hide");
             }
         })
     });
     $(".jPag-pages li").click(function(){
         var page = $(this).text();
         readpinlun(page);
   })
     
	$("#pcc_tishi").click(function(){
		var pinlun=$('#comment_content').val();
		if(pinlun==""){
		    alert("请先评论，后提交");
			return false;
		}
	 $.ajax({
             type : "post",
             url : linkurl + "insertPinglun",
             data: {articleId:id,types:2,bid:"0",cid:0,content:pinlun},
             dataType: 'json',
             success:function (Result) {
                    var data =Result.data;
                   if(Result.result==-1){
                        alert(Result.message);
                        location.href="../login.html";
                   }else if(Result.result==0){
                        alert(Result.message);
                   }else{
                         $('#comment_content').val("");
				         toastr.options = {
								  "closeButton": true,
								  "debug": true,
								  "progressBar": false,
								  "positionClass": "toast-top-center",
								  "showDuration": "500",
								  "hideDuration": "500",
								  "timeOut": "2000",
								  "extendedTimeOut": "1000",
								  "showEasing": "swing",
								  "hideEasing": "linear",
								  "showMethod": "fadeIn",
								  "hideMethod": "fadeOut"
								};  
			            toastr.success('请等待审核');

                   }
               }
          }) 
		

	})
    
});

    function dateFormatUtil(longTypeDate){
        var dateTypeDate = "";
        var date = new Date();
        date.setTime(longTypeDate);
        dateTypeDate += date.getFullYear();    
        dateTypeDate += "-" + getMonth(date);  
        dateTypeDate += "-" + getDay(date);    
        return dateTypeDate;
    }
    function getMonth(date){
        var month = "";
        month = date.getMonth() + 1;  
        if(month<10){
            month = "0" + month;
        }
        return month;
    }
    function getDay(date){
        var day = "";
        day = date.getDate();
        if(day<10){
            day = "0" + day;
        }
        return day;
    }

    function tiaojiao(){
        var cd=$('#cid').val();
        var bd=$('#uid').val();
        var pinglun=$('#pcc_textarea').val();
        $.ajax({
                 type : "post",
                 url : linkurl + "insertPinglun",
                 data: {articleId:id,types:2,bid:bd,cid:cd,content:pinglun},
                 dataType: 'json',
                 success:function (Result) {
                        var data =Result.data;
                       if(Result.result==-1){
                            alert(Result.message);
                       }else if(Result.result==0){
                            alert(Result.message);
                       }else{   
 				       
 						toastr.options = {
 								  "closeButton": true,
 								  "debug": true,
 								  "progressBar": false,
 								  "positionClass": "toast-top-center",
 								  "showDuration": "500",
 								  "hideDuration": "500",
 								  "timeOut": "2000",
 								  "extendedTimeOut": "1000",
 								  "showEasing": "swing",
 								  "hideEasing": "linear",
 								  "showMethod": "fadeIn",
 								  "hideMethod": "fadeOut"
 								};  
                           toastr.success('请等待审核');

                       }
                      }
              }) 
     }
    function sendMsg(cid,bid){ 
        $("#pinglun_modal").modal("show"); 
        $('#cid').val(cid);
        $('#uid').val(bid); 
      }
 
    function bottom(bid,cid){
        $.ajax({
         type : "post",
         url : linkurl + "getqianduanpinglun",
         data: {articleId:id,types:2,bid:bid,cid:cid,page:page,pagesize:100},
         dataType: 'json',
         async:false,
         success:function (Result) {
             var data =Result.data;
             var bom="";
              $.each(data, function (index,item) {
                   bom=bom+("<div class='comment rel bb pcc-padding10'>"+
                         "<img class='avat abs pcc-top0' src='../upload/image/"+item.touxiang+"'>"+
                         "<div class='username c333'>"+item.username+" <em>"+dateFormatUtil(item.createTime*1000)+"</em></div>"+
                         "<div class='txt'>"+item.content+"</div>"+
                     "</div>")
                   })
                   $('#'+cid).after(bom);  
              }
      }) 
 }
    function readpinlun(page){
        $.ajax({
                type : "post",
                url : linkurl + "getqianduanpinglun",
                data: {articleId:id,types:2,bid:"0",cid:0,page:page,pagesize:6},
                dataType: 'json',
                async:false,
                success:function (Result) {
                	$('#comment_list').html("");
                    var data =Result.data;
                    if(Result.result==1){
                        countpage=data[0].pageCount;
                    }
                    var html="";
                     $.each(data, function (index,item) {
                          $('#comment_list').append("<div class='comment rel bb'>"+
                               "<img class='avat abs' src='../upload/image/"+item.touxiang+"'>"+
                               "<div class='username c333'>"+item.username+" <em>"+dateFormatUtil(item.createTime*1000)+"</em></div>"+
                               "<div class='txt'><p id='"+item.id+"'>"+item.content+"</p>"+
                               " <button class='pcc-pinlun1'><a href=\"JavaScript:sendMsg("+item.id+",'"+item.uid+"')\" class='a_color'>回复</a></button>"+
                               "</div>"+
                               "</div>")
                               bottom(item.uid,item.id);
                          })
                     }
             })
        } 